package com.dl.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author DL
 * @create 2021-05-26 15:26
 */
@RequiresUser
@Controller
@Api(tags="shiro操作接口")
@RequestMapping("shiro")
public class ShiroController {
    //备用通用访问
    @GetMapping("/{str}")
    public String toStr(@PathVariable String str){
        return "shiro/" + str;
    }

    @ApiOperation("role")
    @GetMapping("/role")
    @ResponseBody
    public String role(){
        Subject subject = SecurityUtils.getSubject();
        if (subject.hasRole("admin")) {
            return "操作成功";
        }else{
            return "没有权限";
        }
    }

    @ApiOperation("zhujie")
    @GetMapping("/zhujie")
    @ResponseBody
    @RequiresRoles(value={"admin"},logical= Logical.OR)
    public String zhujie(){
        return "操作成功";
    }
}
